//
//  DDTabQuestionScrollerVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/7/5.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDTabQuestionScrollerVC.h"
#import "DDPopResultVC.h"
#import "UIViewController+CWPopup.h"
#import "DDNetWorkAPIClient.h"
#import "TNColorScrollViewController.h"
@interface DDTabQuestionScrollerVC ()
@property (nonatomic, strong) TNColorScrollViewController *colorScrollVC;
@end

@implementation DDTabQuestionScrollerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
}

- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"答题", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [self AddRightBtnAction:@"提交" normal:nil selected:nil action:^{
        
        
        DDPopResultVC *pop = [[DDPopResultVC alloc] init];
        
        [self presentPopupViewController:pop animated:YES completion:^(void) {
            NSLog(@"popup view presented");
        }];
        
        pop.SubmitButtonBlock = ^(NSString *name,NSString *schoolName,NSString *clzssName){
            DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc]init];
            //DDSharedUser *sharedUser = [DDSharedUser shared];
            //if (sharedUser.userInfo.name==nil ||[@"" isEqualToString:sharedUser.userInfo.name]) {
            //提交用户信息
//            NSDictionary *params = @{@"school":schoolName,@"name":name,@"clzss":clzssName};
            [network userEdit:@"" nickName:@"" name:name gender:@"" birthday:@"" phoneNum:@"" school:schoolName grade:@"" clzss:clzssName recent:@"" completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
//            [network userEdit:params completionBlock:^(BOOL isSuccess,NSString *message, NSString *status) {
                DDSharedUser *sharedUser = [DDSharedUser shared];
                
                sharedUser.userInfo.name = name;
                sharedUser.userInfo.school = schoolName;
                sharedUser.userInfo.clzss = clzssName;
            }];
            
            //}
            
            //提交题目
            [network mainAnswerSave:@{@"topicId":_topicID,@"qid":_question.id,@"answer":@"A",@"costSeconds":@"10"} completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
                NSLog(@"%@",object);
                if (object) {
                    [self dismissPopup];
                }
                [MBProgressHUD showMessag:message toView:self.view];
                
            }];
        };
        
        
    }];
    

}
-(void)createData
{
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(dismissPopup) name:@"dismissPopup" object:nil];
    // Override point for customization after application launch.
    
    TNColorViewData *view1Data = [[TNColorViewData alloc] init];
    view1Data.color = [UIColor colorWithRed:254/255.0f green:206/255.0f blue:48/255.0f alpha:1];
    
    TNColorViewData *view2Data = [[TNColorViewData alloc] initWithColor:[UIColor colorWithRed:230/255.0f green:177/255.0f blue:53/255.0f alpha:1]];
    
    TNColorViewData *view3Data = [[TNColorViewData alloc] initWithColor:[UIColor colorWithRed:253/255.0f green:169/255.0f blue:41/255.0f alpha:1]];
    
    TNColorViewData *view4Data = [[TNColorViewData alloc] init];
    view4Data.color = [UIColor colorWithRed:229/255.0f green:137/255.0f blue:37/255.0f alpha:1];
    
    //TNColorViewData *view5Data = [[TNColorViewData alloc] initWithCustomClass:[MyCustomViewClass class]];
    
    NSArray *colorData = @[view1Data, view2Data, view3Data, view4Data];
    
    TNColorScrollViewConfig *config = [[TNColorScrollViewConfig alloc] initWithColorDataObjects:colorData bounces:YES showsHorizontalScrollIndicator:NO showsVerticalScrollIndicator:NO pagingEnabled:YES scrollHorizontal:YES];
    
    self.colorScrollVC = [[TNColorScrollViewController alloc] initWithColorScrollViewConfig:config];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dismissPopup {
    if (self.popupViewController != nil) {
        [self dismissPopupViewControllerAnimated:YES completion:^{
            NSLog(@"popup view dismissed");
        }];
    }
}

@end
